package SocialMediaSentiment;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import jxl.write.WriteException;

public class updateCounters {

	public static void updateCounterFile(String counterFile,
			String wrongClassi, List<String> significantWords)
			throws IOException, WriteException {

		HashMap<String, String> wordsCounter = filetoHashMap(counterFile);
		HashMap<Integer, String[]> indexTweetclassi = ReadExcel.read(
				wrongClassi, 0, 1);
		Iterator<Integer> k2 = (Iterator<Integer>) indexTweetclassi.keySet()
				.iterator();
		while (k2.hasNext()) {
			String tweet = indexTweetclassi.get(k2.next())[0];
			StringTokenizer tokenizer = new StringTokenizer(tweet, " ");
			while (tokenizer.hasMoreTokens()) {
				String word = tokenizer.nextToken();
				if (significantWords.contains(word)) {
					try{wordsCounter
							.put(word,
									""
											+ (Integer.parseInt(wordsCounter
													.get(word)) + 1));
					}
					catch( NumberFormatException e )
					{
						System.out.println(word);
						wordsCounter.put(word,  ""+1);
					}
				}
			}
		}
		writeExcel.write(counterFile, wordsCounter);
	}

	public static HashMap<String, String> filetoHashMap(String filename)
			throws IOException {
		HashMap<Integer, String[]> indexTweetCounter = ReadExcel.read(filename,
				0, 1);
		Iterator<Integer> k = (Iterator<Integer>) indexTweetCounter.keySet()
				.iterator();
		HashMap<String, String> hashMap = new HashMap<String, String>();
		while (k.hasNext()) {
			String[] data = indexTweetCounter.get(k.next());
			hashMap.put(data[0], data[1]);
		}
		return hashMap;
	}

}
